<?php
/*
 * 检查用户登陆状态
 */
if(!function_exists('is_login')){
	function is_login(){
		global $isLogin;
		$isLogin = false;
		$loginCookie = substr(cookie('ajwUid'),0,-1);
		if(isset($loginCookie) && $loginCookie){
			if(!session('loginUid') || !session('loginName') || !session('role_id')){
				return false;exit;
			}
			$isLogin = true;
		}
		return $isLogin;
	}
}

/*
 * 获取登陆用户的登陆信息
 */
if(!function_exists('getLoginMsg')){
	function getLoginMsg(){
		global $isLogin;
		$loginMsgObj = new stdClass();
		$loginMsgObj->isLogin = $isLogin;
		$loginMsgObj->username = base64_decode(substr(cookie('ajwUid'),0,-1));
		$users = M('ResEntity')->where(array('username'=>$loginMsgObj->username))->find();
		$loginMsgObj->rid = $users['rid'];
		$loginMsgObj->email = $users['email'];
		
		return $loginMsgObj;
	}
}

/*
 * 返回随机字符串
 */
if(!function_exists('randstr')){
	function randstr($num=1){
		$return = '';
		$randstr = '1023456789abcdefghijklmnopqrstuvwxyz';
		for($i=0;$i<$num;$i++){
			$return .= $randstr{rand(0,35)};
		}
		
		return $return;
	}
}



/*
 * 日志添加记录
 */
if(!function_exists('putlog')){
	function putlog($msg,$controller,$fun){
		M('SysLogs')->add(array(
			'sys_controller_name' => $controller,
			'sys_function_name' => $fun,
			'sys_remark' => (I('post.username') ? I('post.username') : '').$msg,
			'sys_editor' => getLoginMsg()->rid,
			'add_at' => time(),
			'update_at' => time()
		));
	}
}


/*
 * JSON格式化输出
 */
if(!function_exists('dataFormat')){
	function dataFormat($data=array(),$recode=1,$msg='',$adds=array()){
		if($data || $adds){
			echo json_encode(array_merge(array(
					'data' => $data,
					'code' => $recode,
					'msg' => $msg ? $msg : 'success'
			),$adds));exit;
		}else{
			echo json_encode(array_merge(array(
					'code' => $recode,
					'msg' => $msg ? $msg : '没有相关数据'
			)));exit;
		}
	}
}
	/*
	 * Excel导入功能
	 * */
	if(!function_exists('importExcel')){
		function importExcel($url){
			if(!file_exists($url)){
				exit('文件目录不存在');
			}
			return (new \service\excel\PhpExcel())->importExcel($url);
		}
	}

	/**
	 * 图片上传七牛
	 */
	if(!function_exists('uploadQiniu')){
		function uploadQiniu($img,$name=''){
			$filePath = $img;
			$explode = explode('.', $filePath);
			$type = $explode[count($explode)-1];
			$key = $name.'_'.time().'_'.rand(1000,9999).'.'.$type;
			// 要上传文件的本地路径
			$data = (new \Org\Util\Qiniu())->upFile($filePath,$key);
			$qiniu_file_url = QINIUHOST.$data['key'];
			if($qiniu_file_url){
				unlink($filePath);
			}
			return $qiniu_file_url;
		}
	}

?>